<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 默认的提交事件 -->
    <!-- <form action="http://www.baidu.com" onsubmit="return false"> -->
    <form action="http://www.baidu.com">
        账号: <input type="text" placeholder="请输入账号" id="username" name="username"><br>
        密码: <input type="text" placeholder="请输入密码" id="password" name="password"><br>
        <input type="submit">
    </form>
    <script>
        const form = document.querySelector("form");
        form.onsubmit = function(e){
            const username = document.querySelector("#username").value
            const password = document.querySelector("#password").value

            console.log(username,password);

            e = e || window.event
            e.preventDefault();
            // //使用内置函数创建一个ajax对象
            const xhr = new XMLHttpRequest();
            /* 兼容性 */
            // const x = new ActiveXObject('Microsoft.XMLHTTP');
            //打开链接,准备发送请求
            xhr.open("GET",`./login.php?username=${username}&password=${password}`)
            //发送请求
            xhr.send();

            //监听readystate
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4 && /2\d{2}/.test(xhr.status)){
                    window.alert(xhr.responseText)
                }
            }
            // return false;
        }
    </script>
</body>
</html>